<?php

declare(strict_types=1);

namespace app\common\listener;

use think\Container;
use think\facade\Log;

class WsEvent
{
    protected Container $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function handle($event): void
    {
        $type = $event->type;
        try {
            switch ($type) {
                case 'group':
                    // 实例化 GroupScreen 并执行 handle 方法
                    $groupScreen = $this->container->make(GroupScreen::class);
                    $groupScreen->handle($event);
                    break;
                case 'company':
                    // 实例化 CompanyScreen 并执行 handle 方法
                    $companyScreen = $this->container->make(CompanyScreen::class);
                    $companyScreen->handle($event);
                    break;
                case 'ping':
                    // 实例化 Ping 并执行 handle 方法
                    $ping = $this->container->make(Ping::class);
                    $ping->handle($event);
                    break;
                default:
                    Log::info('ws_event_type_error:' . $type . ';未知事件类型！');
                    echo 'ws_event_type_error:' . $type . ';未知事件类型！';
                    break;
            }
        } catch (\Throwable $e) {
            Log::info('ws_event_error:' . $e->getMessage());
        }
    }
}